

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="排序文本sort语法12sort [OPTION]... [FILE]...sort [OPTION]... --files0-from&#x3D;F  描述1、将所有文件的排序连接写入标准输出。 2、如果没有文件，或者当文件为-时，读取标准输入。 sort 命令的 行排序使用 sort 命令排序文本 12345678910111213141516171819202122232425262728293031">
<meta property="og:type" content="article">
<meta property="og:title" content="shell-基本文本处理">
<meta property="og:url" content="http://example.com/2019/04/16/shell/shell-%E5%9F%BA%E6%9C%AC%E6%96%87%E6%9C%AC%E5%A4%84%E7%90%86/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="排序文本sort语法12sort [OPTION]... [FILE]...sort [OPTION]... --files0-from&#x3D;F  描述1、将所有文件的排序连接写入标准输出。 2、如果没有文件，或者当文件为-时，读取标准输入。 sort 命令的 行排序使用 sort 命令排序文本 12345678910111213141516171819202122232425262728293031">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-04-16T02:01:05.000Z">
<meta property="article:modified_time" content="2022-02-10T03:11:13.444Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="Shell">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>shell-基本文本处理 - Hexo</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.13","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>LZDF</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="shell-基本文本处理">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2019-04-16 10:01" pubdate>
        2019年4月16日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      22k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      181 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">shell-基本文本处理</h1>
            
            <div class="markdown-body">
              <h1 id="排序文本"><a href="#排序文本" class="headerlink" title="排序文本"></a>排序文本</h1><h3 id="sort"><a href="#sort" class="headerlink" title="sort"></a>sort</h3><h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs bash">sort [OPTION]... [FILE]...<br>sort [OPTION]... --files0-from=F<br></code></pre></td></tr></table></figure>

<h4 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h4><p>1、将所有文件的排序连接写入标准输出。</p>
<p>2、如果没有文件，或者当文件为-时，读取标准输入。</p>
<h2 id="sort-命令的-行排序"><a href="#sort-命令的-行排序" class="headerlink" title="sort 命令的 行排序"></a>sort 命令的 行排序</h2><p><strong>使用 sort 命令排序文本</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ cat fruits.txt<br>banana<br>orange<br>Apple<br>Persimmon<br>apple<br>%%banana<br>apple<br>ORANGE<br>$ LANG=En_US sort fruits.txt <span class="hljs-comment"># ASCII 排序：ASCII中，%在大写字母前，大写字母在小写字母前</span><br>%%banana<br>Apple<br>ORANGE<br>Persimmon<br>apple<br>apple<br>banana<br>orange<br>$ sort fruits.txt <span class="hljs-comment"># 普通排序</span><br>apple<br>apple<br>Apple<br>banana<br>%%banana<br>orange<br>ORANGE<br>Persimmon<br>$ sort -d fruits.txt	<span class="hljs-comment"># 字典排序</span><br>apple<br>apple<br>Apple<br>banana<br>%%banana<br>orange<br>ORANGE<br>Persimmon<br>$ sort -d -f fruits.txt <span class="hljs-comment"># -f 忽略大小写差异</span><br>apple<br>apple<br>Apple<br>banana<br>%%banana<br>orange<br>ORANGE<br>Persimmon<br>$ sort -d -f -u fruits.txt <span class="hljs-comment"># -u 去除重复项</span><br>Apple<br>banana<br>orange<br>Persimmon<br></code></pre></td></tr></table></figure>

<h2 id="sort-命令的字段排序"><a href="#sort-命令的字段排序" class="headerlink" title="sort 命令的字段排序"></a>sort 命令的字段排序</h2><p><code>sort</code> 命令还可以对字段进行排序，在 <code>sort</code> 的参数列表中，<code>-k</code> 参数可以选定排序字段，<code>-t</code> 参数可以选择字段分界符，默认是空白符。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-comment"># 字段排序</span><br>$ cat /etc/group<br>root:x:0:<br>sudo:x:27:aurora<br>www-data:x:33:<br>games:x:60:<br>users:x:100:aurora<br>aurora:x:1000:<br>nobody:x:998:<br>docker:x:996:aurora<br>mysql:x:1001:<br>redis:x:125:<br>$ sort -t: -k3 -m /etc/group<br>root:x:0:<br>sudo:x:27:aurora<br>www-data:x:33:<br>games:x:60:<br>users:x:100:aurora<br>redis:x:125:<br>vboxusers:x:126:<br>docker:x:996:aurora<br>nobody:x:998:<br>aurora:x:1000:<br>mysql:x:1001:<br><br></code></pre></td></tr></table></figure>

<h1 id="文本去重"><a href="#文本去重" class="headerlink" title="文本去重"></a>文本去重</h1><h3 id="uniq"><a href="#uniq" class="headerlink" title="uniq"></a>uniq</h3><p>UNIX/Linux 系统中的另一条用于数据记录去重的命令。<code>uniq</code> 命令去除数据流中重复的记录，只留下第一条记录。</p>
<h4 id="语法-1"><a href="#语法-1" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">uniq [ -c | -d | -u ] [ InFile [ OutFile ]]<br></code></pre></td></tr></table></figure>

<h4 id="描述-1"><a href="#描述-1" class="headerlink" title="描述"></a>描述</h4><p><code>uniq</code> 命令读取由 <code>InFile</code> 参数指定的标准输入或文件，删除重复行。该命令首先比较相邻的行，然后去除第二行和该行的后续副本。<strong>重复的行一定相邻</strong>（在使用 <code>uniq</code> 命令之前，要先使用 <code>sort</code> 命令，使所有重复行相邻）。最后，<code>uniq</code> 将最终单独的行写入输出或由 <code>OutFile</code> 参数指定的文件。<code>InFile</code> 和 <code>OutFile</code> 参数必须指定不同的文件。</p>
<p>输入文件必须是文本文件，且文本文件中一行的长度不能超过 2048 个字节（包含所有换行符），并且其中不能包含空字符。</p>
<p>执行成功，返回0；失败，返回值大于0。</p>
<h4 id="标志"><a href="#标志" class="headerlink" title="标志"></a>标志</h4><ul>
<li><strong>-c</strong> 在输出行前面加上每行在输入文件中出现的次数。</li>
<li><strong>-d</strong> 仅显示重复行。</li>
<li><strong>-u</strong> 仅显示不重复的行。</li>
</ul>
<h4 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h4><p>删除重复行：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash">uniq file.txt<br>sort file.txt | uniq<br>sort -u file.txt<br></code></pre></td></tr></table></figure>

<p>只显示但一行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs bash">uniq -u file.txt<br>sort file.txt | uniq -u<br></code></pre></td></tr></table></figure>

<p>统计各行在文件中出现的次数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">sort file.txt | uniq -c<br></code></pre></td></tr></table></figure>

<p>在文件中找出重复的行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">sort file.txt | uniq -d<br></code></pre></td></tr></table></figure>

<p><em><code>uniq</code> 命令常常与其他工具结合使用，来去除文本中的冗余。</em></p>
<h1 id="统计文本行数、字数以及字符数"><a href="#统计文本行数、字数以及字符数" class="headerlink" title="统计文本行数、字数以及字符数"></a>统计文本行数、字数以及字符数</h1><h3 id="wc"><a href="#wc" class="headerlink" title="wc"></a>wc</h3><p>UNIX/Linux 中的 <code>wc</code> 命令可以提供文本的行数、字数、字符数统计。</p>
<p><strong>示例1</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ wc /etc/passwd			<span class="hljs-comment"># 28行、54个单词、1411个字符</span><br>28   54 1411 /etc/passwd<br>$ wc -c /etc/passwd			<span class="hljs-comment"># 1411个字符</span><br>1411 /etc/passwd<br>$ wc -w /etc/passwd			<span class="hljs-comment"># 54个单词</span><br>54 /etc/passwd<br>$ wc -l /etc/passwd			<span class="hljs-comment"># 28行</span><br>28 /etc/passwd<br></code></pre></td></tr></table></figure>
<p><strong>示例2</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ find /etc -iname <span class="hljs-string">&quot;*.conf&quot;</span> | wc -l	<span class="hljs-comment"># 找出 /etc 文件夹下 conf 文件的个数</span><br>228<br>$ grep <span class="hljs-string">&quot;bash&quot;</span> /etc/passwd | wc -l	<span class="hljs-comment"># 找出 /etc/passwd 文件中包含 bash 字符串的行数</span><br>3<br></code></pre></td></tr></table></figure>

<p>其实 <code>... grep str | wc -l</code> 有更简便的写法，它已经被集成到了 <code>grep</code> 命令，通过参数 <code>-c</code> 实现：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ grep <span class="hljs-string">&quot;bash&quot;</span> /etc/passwd | wc -l<br>3<br>$ grep -c <span class="hljs-string">&quot;bash&quot;</span> /etc/passwd	<span class="hljs-comment"># 这两条命令实现了相同的效果</span><br>3<br></code></pre></td></tr></table></figure>

<p><code>wc</code> 还可以同时统计多个文件中的数据，并且将统计结果汇总。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ wc /etc/*rc<br>   93   376  3003 /etc/bashrc<br>   72   204  1620 /etc/csh.cshrc<br>   42   114   942 /etc/inputrc<br>  216   868  6722 /etc/screenrc<br>   64   283  1982 /etc/vimrc<br>   64   283  1982 /etc/virc<br>  125   794  4479 /etc/wgetrc<br>  676  2922 20730 总用量<br></code></pre></td></tr></table></figure>

<p><em><code>wc</code> 命令统计 <code>/etc</code> 下所有以 <code>rc</code> 结尾的文件，统计他们中的字符数、单词数和行数，并且在最后一行将总计的结果打印出来。</em></p>
<h1 id="打印和格式化输出"><a href="#打印和格式化输出" class="headerlink" title="打印和格式化输出"></a>打印和格式化输出</h1><h2 id="使用-pr-打印文件"><a href="#使用-pr-打印文件" class="headerlink" title="使用 pr 打印文件"></a>使用 <code>pr</code> 打印文件</h2><h3 id="pr"><a href="#pr" class="headerlink" title="pr"></a>pr</h3><p>UNIX/Linux 的 <code>pr</code> 命令可以用来将文本转换成适合打印的文件。</p>
<p>这个工具的基本用途就是将较大的文件分割成多个页面，并为每个页面添加标题。</p>
<h4 id="语法-2"><a href="#语法-2" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ pr --<span class="hljs-built_in">help</span><br>用法：pr [选项]... [文件]...<br>如果没有指定文件，或者文件为<span class="hljs-string">&quot;-&quot;</span>，则从标准输入读取。<br><br>必选参数对长短选项同时适用。<br>  +首页[:末页], --pages=首页[:末页]<br>			在指定的首页/末页处开始/停止打印<br>  -列数, --columns=列数<br>			输出指定的列数。如果指定了-a 选项，则从上到下列印。<br>			程序会自动在每一页均衡每列占用的行数。<br>  -a, --across		设置每列从上到下输出，配合<span class="hljs-string">&quot;-列数&quot;</span>选项一起使用<br>  -c, --show-control-chars<br>			使用头标(^G)和八进制反斜杠标记<br>  -d, --double-space	加倍输出空白区域<br>  -D, --date-format=格式<br>			使用遵循指定格式的页眉日期<br>  -e[字符[宽度]], --expand-tabs[=字符[宽度]]<br>			扩展输入的字符(制表符) 到制表符宽度(8)<br>  -F, -f, --form-feed	使用出纸页页标代替新行作为页面间的分隔符<br>			(使用-F 选项时报头为3 行,不使用时为5 行)<br>  -h, --header=页眉	在页眉中使用居中的指定字符代替文件名<br>			-h <span class="hljs-string">&quot;&quot;</span> 输出一个空行，不要使用 -h<span class="hljs-string">&quot;&quot;</span><br>  -i[字符[宽度]], --output-tabs[=字符[宽度]]<br>			使用指定字符(或制表符)代替空格不足到指定制表符宽度(默认8)<br>  -J, --join-lines	合并整个行，关闭-W 选项的行截断，不使用栏调整，使用<br>				--sep-string[=字符串] 设置分隔符<br>  -l, --length=PAGE_LENGTH<br>                    <span class="hljs-built_in">set</span> the page length to PAGE_LENGTH (66) lines<br>                    (default number of lines of text 56, and with -F 63).<br>                    implies -t <span class="hljs-keyword">if</span> PAGE_LENGTH &lt;= 10<br>  -m, --merge       <span class="hljs-built_in">print</span> all files <span class="hljs-keyword">in</span> parallel, one <span class="hljs-keyword">in</span> each column,<br>                    truncate lines, but join lines of full length with -J<br>  -n[分隔符[位数]], --number-lines[=分隔符[位数]]<br>			显示行号，使用指定(默认5) 位数，后接分隔符(默认TAB)<br>			默认从输入文件的第一行开始计数<br>  -N, --first-line-number=数字<br>			从首页的首行以指定数字开始计数(参看<span class="hljs-string">&quot;+首页&quot;</span>)<br>  -o, --indent=缩进量<br>			将每行缩进(默认0)个空格，不影响-w 或-W 参数，<br>			缩进亮的值将被加入页面宽度<br>  -r, --no-file-warnings<br>			当文件无法打开时忽略警告<br>  -s[CHAR], --separator[=CHAR]<br>                    separate columns by a single character, default <span class="hljs-keyword">for</span> CHAR<br>                    is the &lt;TAB&gt; character without -w and <span class="hljs-string">&#x27;no char&#x27;</span> with -w.<br>                    -s[CHAR] turns off line truncation of all 3 column<br>                    options (-COLUMN|-a -COLUMN|-m) except -w is <span class="hljs-built_in">set</span><br>  -S[STRING], --sep-string[=STRING]<br>                    separate columns by STRING,<br>                    without -S: Default separator &lt;TAB&gt; with -J and &lt;space&gt;<br>                    otherwise (same as -S<span class="hljs-string">&quot; &quot;</span>), no effect on column options<br>  -t, --omit-header  omit page headers and trailers;<br>                     implied <span class="hljs-keyword">if</span> PAGE_LENGTH &lt;= 10<br>  -T, --omit-pagination<br>			按照输入文件中的设置忽略页眉和页脚并除去所有分页记号<br>  -v, --show-nonprinting<br>			使用八进制反斜杠标记<br>  -w, --width=页面宽度<br>			为多栏页面输出将设置为指定的字符数(默认72)，<br>			仅当-s[char] 选项不启用时有效(即保持默认值 72)。<br>  -W, --page-width=页宽<br>			总是将页宽设置为指定的(默认72)字符数，<br>			除非-J 选项启用总是截断行，此参数与-S 或-s 冲突<br>      --<span class="hljs-built_in">help</span>		显示此帮助信息并退出<br>      --version		显示版本信息并退出<br></code></pre></td></tr></table></figure>

<p><strong>示例</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ cat /tmp/a.txt<br>TUBITAK Kamu SM SSL Kok Sertifikasi - Surum 1<br>=============================================<br>-----BEGIN CERTIFICATE-----<br>MIIEYzCCA0ugAwIBAgIBATANBgkqhkiG9w0BAQsFADCB0jELMAkGA1UEBhMCVFIxGDAWBgNVBAcT<br>D0dlYnplIC0gS29jYWVsaTFCMEAGA1UEChM5VHVya2l5ZSBCaWxpbXNlbCB2ZSBUZWtub2xvamlr<br>IEFyYXN0aXJtYSBLdXJ1bXUgLSBUVUJJVEFLMS0wKwYDVQQLEyRLYW11IFNlcnRpZmlrYXN5b24g<br>TWVya2V6aSAtIEthbXUgU00xNjA0BgNVBAMTLVRVQklUQUsgS2FtdSBTTSBTU0wgS29rIFNlcnRp<br>ZmlrYXNpIC0gU3VydW0gMTAeFw0xMzExMjUwODI1NTVaFw00MzEwMjUwODI1NTVaMIHSMQswCQYD<br>VQQGEwJUUjEYMBYGA1UEBxMPR2ViemUgLSBLb2NhZWxpMUIwQAYDVQQKEzlUdXJraXllIEJpbGlt<br>c2VsIHZlIFRla25vbG9qaWsgQXJhc3Rpcm1hIEt1cnVtdSAtIFRVQklUQUsxLTArBgNVBAsTJEth<br>bXUgU2VydGlmaWthc3lvbiBNZXJrZXppIC0gS2FtdSBTTTE2MDQGA1UEAxMtVFVCSVRBSyBLYW11<br>IFNNIFNTTCBLb2sgU2VydGlmaWthc2kgLSBTdXJ1bSAxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A<br>MIIBCgKCAQEAr3UwM6q7a9OZLBI3hNmNe5eA027n/5tQlT6QlVZC1xl8JoSNkvoBHToP4mQ4t4y8<br>6Ij5iySrLqP1N+RAjhgleYN1Hzv/bKjFxlb4tO2KRKOrbEz8HdDc72i9z+SqzvBV96I01INrN3wc<br>wv61A+xXzry0tcXtAA9TNypN9E8Mg/uGz8v+jE69h/mniyFXnHrfA2eJLJ2XYacQuFWQfw4tJzh0<br>3+f92k4S400VIgLI4OD8D62K18lUUMw7D8oWgITQUVbDjlZ/iSIzL+aFCr2lqBs23tPcLG07xxO9<br>WSMs5uWk99gL7eqQQESolbuT1dCANLZGeA4fAJNG4e7p+exPFwIDAQABo0IwQDAdBgNVHQ4EFgQU<br>ZT/HiobGPN08VFw1+DrtUgxHV8gwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wDQYJ<br>KoZIhvcNAQELBQADggEBACo/4fEyjq7hmFxLXs9rHmoJ0iKpEsdeV31zVmSAhHqT5Am5EM2fKifh<br>AHe+SMg1qIGf5LgsyX8OsNJLN13qudULXjS99HMpw+0mFZx+CFOKWI3QSyjfwbPfIPP54+M638yc<br>lNhOT8NrF7f3cuitZjO1JVOr4PhMqZ398g26rrnZqsZr+ZO7rqu4lzwDGrpDxpa5RXI4s6ehlj2R<br>e37AIVNMh+3yC1SVUZPVIqUNivGTDj5UDrDYyU7c8jEyVupk+eq1nRZmQnLzf9OxMUP8pI4X8W0j<br>q5Rm+K37DwhuJi1/FwcJsoz7UMCflo3Ptv0AnVoUmr8CRPXBwp8iXqIPoeM=<br>-----END CERTIFICATE-----<br><br>$ pr -h <span class="hljs-string">&quot;pr打印输出文件&quot;</span>  /tmp/a.txt <br><br><br>2019-04-24 15:04                 pr打印输出文件                  第 1 页<br><br><br>TUBITAK Kamu SM SSL Kok Sertifikasi - Surum 1<br>=============================================<br>-----BEGIN CERTIFICATE-----<br>MIIEYzCCA0ugAwIBAgIBATANBgkqhkiG9w0BAQsFADCB0jELMAkGA1UEBhMCVFIxGDAWBgNVBAcT<br>D0dlYnplIC0gS29jYWVsaTFCMEAGA1UEChM5VHVya2l5ZSBCaWxpbXNlbCB2ZSBUZWtub2xvamlr<br>IEFyYXN0aXJtYSBLdXJ1bXUgLSBUVUJJVEFLMS0wKwYDVQQLEyRLYW11IFNlcnRpZmlrYXN5b24g<br>TWVya2V6aSAtIEthbXUgU00xNjA0BgNVBAMTLVRVQklUQUsgS2FtdSBTTSBTU0wgS29rIFNlcnRp<br>ZmlrYXNpIC0gU3VydW0gMTAeFw0xMzExMjUwODI1NTVaFw00MzEwMjUwODI1NTVaMIHSMQswCQYD<br>VQQGEwJUUjEYMBYGA1UEBxMPR2ViemUgLSBLb2NhZWxpMUIwQAYDVQQKEzlUdXJraXllIEJpbGlt<br>c2VsIHZlIFRla25vbG9qaWsgQXJhc3Rpcm1hIEt1cnVtdSAtIFRVQklUQUsxLTArBgNVBAsTJEth<br>bXUgU2VydGlmaWthc3lvbiBNZXJrZXppIC0gS2FtdSBTTTE2MDQGA1UEAxMtVFVCSVRBSyBLYW11<br>IFNNIFNTTCBLb2sgU2VydGlmaWthc2kgLSBTdXJ1bSAxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A<br>MIIBCgKCAQEAr3UwM6q7a9OZLBI3hNmNe5eA027n/5tQlT6QlVZC1xl8JoSNkvoBHToP4mQ4t4y8<br>6Ij5iySrLqP1N+RAjhgleYN1Hzv/bKjFxlb4tO2KRKOrbEz8HdDc72i9z+SqzvBV96I01INrN3wc<br>wv61A+xXzry0tcXtAA9TNypN9E8Mg/uGz8v+jE69h/mniyFXnHrfA2eJLJ2XYacQuFWQfw4tJzh0<br>3+f92k4S400VIgLI4OD8D62K18lUUMw7D8oWgITQUVbDjlZ/iSIzL+aFCr2lqBs23tPcLG07xxO9<br>WSMs5uWk99gL7eqQQESolbuT1dCANLZGeA4fAJNG4e7p+exPFwIDAQABo0IwQDAdBgNVHQ4EFgQU<br>ZT/HiobGPN08VFw1+DrtUgxHV8gwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wDQYJ<br>KoZIhvcNAQELBQADggEBACo/4fEyjq7hmFxLXs9rHmoJ0iKpEsdeV31zVmSAhHqT5Am5EM2fKifh<br>AHe+SMg1qIGf5LgsyX8OsNJLN13qudULXjS99HMpw+0mFZx+CFOKWI3QSyjfwbPfIPP54+M638yc<br>lNhOT8NrF7f3cuitZjO1JVOr4PhMqZ398g26rrnZqsZr+ZO7rqu4lzwDGrpDxpa5RXI4s6ehlj2R<br>e37AIVNMh+3yC1SVUZPVIqUNivGTDj5UDrDYyU7c8jEyVupk+eq1nRZmQnLzf9OxMUP8pI4X8W0j<br>q5Rm+K37DwhuJi1/FwcJsoz7UMCflo3Ptv0AnVoUmr8CRPXBwp8iXqIPoeM=<br>-----END CERTIFICATE-----<br></code></pre></td></tr></table></figure>

<p>使用 <code>pr</code> 格式化后的文件输出，多了日期、标题和页数，以及多个空行部分。</p>
<p><code>-h</code> 标注了打印文件的标题，如果没有使用 <code>-h</code> ，则默认标题就是这个文件的文件名。如果不想显示标题，可以使用 <code>-t</code> 参数。</p>
<h2 id="使用-fmt-命令格式化文本"><a href="#使用-fmt-命令格式化文本" class="headerlink" title="使用 fmt 命令格式化文本"></a>使用 <code>fmt</code> 命令格式化文本</h2><p>除了 <code>pr</code> 命令，UNIX/Linux 下还有一条 <code>fmt</code> 命令可以格式化文本段落，使文本不超出可见的屏幕范围。</p>
<p><code>fmt</code> 命令读取文件的内容，根据选项的设置对文件格式进行简单的优化处理，并将结果送到标准输出设备。</p>
<h3 id="fmt"><a href="#fmt" class="headerlink" title="fmt"></a>fmt</h3><h4 id="语法-3"><a href="#语法-3" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ fmt --<span class="hljs-built_in">help</span><br>用法：fmt [-宽度] [选项]... [文件]...<br>Reformat each paragraph <span class="hljs-keyword">in</span> the FILE(s), writing to standard output.<br>The option -WIDTH is an abbreviated form of --width=DIGITS.<br><br>如果没有指定文件，或者文件为<span class="hljs-string">&quot;-&quot;</span>，则从标准输入读取。<br><br>必选参数对长短选项同时适用。<br>  -c --crown-margin		保持前两行的缩进<br>  -p, --prefix=字符串		只对以指定字符串开头的行重新格式化，<br>				将前缀重新附着到被重新格式化的行上<br>  -s, --split-only		分割过长的行，但不自动补足<br>  -t, --tagged-paragraph    第一行的缩进与第二行不同<br>  -u, --uniform-spacing     单词之间有一个空格，句子后面有两个空格<br>  -w, --width=WIDTH         最大行宽 (默认 75 列)<br>  -g, --goal=WIDTH          goal width (default of 93% of width)<br>      --<span class="hljs-built_in">help</span>		显示此帮助信息并退出<br>      --version		显示版本信息并退出<br></code></pre></td></tr></table></figure>

<p><strong>示例</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ cat /tmp/a.txt | fmt -w 10<br>TUBITAK<br>Kamu SM<br>SSL Kok<br>Sertifikasi<br>- Surum 1<br>=============================================<br>-----BEGIN<br>CERTIFICATE-----<br>MIIEYzCCA0ugAwIBAgIBATANBgkqhkiG9w0BAQsFADCB0jELMAkGA1UEBhMCVFIxGDAWBgNVBAcT<br>D0dlYnplIC0gS29jYWVsaTFCMEAGA1UEChM5VHVya2l5ZSBCaWxpbXNlbCB2ZSBUZWtub2xvamlr<br>IEFyYXN0aXJtYSBLdXJ1bXUgLSBUVUJJVEFLMS0wKwYDVQQLEyRLYW11IFNlcnRpZmlrYXN5b24g<br>TWVya2V6aSAtIEthbXUgU00xNjA0BgNVBAMTLVRVQklUQUsgS2FtdSBTTSBTU0wgS29rIFNlcnRp<br>ZmlrYXNpIC0gU3VydW0gMTAeFw0xMzExMjUwODI1NTVaFw00MzEwMjUwODI1NTVaMIHSMQswCQYD<br>VQQGEwJUUjEYMBYGA1UEBxMPR2ViemUgLSBLb2NhZWxpMUIwQAYDVQQKEzlUdXJraXllIEJpbGlt<br>c2VsIHZlIFRla25vbG9qaWsgQXJhc3Rpcm1hIEt1cnVtdSAtIFRVQklUQUsxLTArBgNVBAsTJEth<br>bXUgU2VydGlmaWthc3lvbiBNZXJrZXppIC0gS2FtdSBTTTE2MDQGA1UEAxMtVFVCSVRBSyBLYW11<br>IFNNIFNTTCBLb2sgU2VydGlmaWthc2kgLSBTdXJ1bSAxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A<br>MIIBCgKCAQEAr3UwM6q7a9OZLBI3hNmNe5eA027n/5tQlT6QlVZC1xl8JoSNkvoBHToP4mQ4t4y8<br>6Ij5iySrLqP1N+RAjhgleYN1Hzv/bKjFxlb4tO2KRKOrbEz8HdDc72i9z+SqzvBV96I01INrN3wc<br>wv61A+xXzry0tcXtAA9TNypN9E8Mg/uGz8v+jE69h/mniyFXnHrfA2eJLJ2XYacQuFWQfw4tJzh0<br>3+f92k4S400VIgLI4OD8D62K18lUUMw7D8oWgITQUVbDjlZ/iSIzL+aFCr2lqBs23tPcLG07xxO9<br>WSMs5uWk99gL7eqQQESolbuT1dCANLZGeA4fAJNG4e7p+exPFwIDAQABo0IwQDAdBgNVHQ4EFgQU<br>ZT/HiobGPN08VFw1+DrtUgxHV8gwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB/wQFMAMBAf8wDQYJ<br>KoZIhvcNAQELBQADggEBACo/4fEyjq7hmFxLXs9rHmoJ0iKpEsdeV31zVmSAhHqT5Am5EM2fKifh<br>AHe+SMg1qIGf5LgsyX8OsNJLN13qudULXjS99HMpw+0mFZx+CFOKWI3QSyjfwbPfIPP54+M638yc<br>lNhOT8NrF7f3cuitZjO1JVOr4PhMqZ398g26rrnZqsZr+ZO7rqu4lzwDGrpDxpa5RXI4s6ehlj2R<br>e37AIVNMh+3yC1SVUZPVIqUNivGTDj5UDrDYyU7c8jEyVupk+eq1nRZmQnLzf9OxMUP8pI4X8W0j<br>q5Rm+K37DwhuJi1/FwcJsoz7UMCflo3Ptv0AnVoUmr8CRPXBwp8iXqIPoeM=<br>-----END<br>CERTIFICATE-----<br></code></pre></td></tr></table></figure>

<p><code>-w 10</code> 告诉 <code>fmt</code> 命令每行最大字符数。当 <code>fmt</code> 命令在打印时发现某行已经达到最大字符数，但是某个单词为呢过完全显示，则 <code>fmt</code> 将该单词置于下一行来显示。</p>
<p><strong>Notice：</strong> <em>无论是 <code>pr</code> 还是 <code>fmt</code>，在不同版本的系统中的行为都不尽相同。因此需要在不同版本的系统上查阅 manpage，来确定格式化输出工具的功能。</em></p>
<h2 id="使用-fold-限制文本宽度"><a href="#使用-fold-限制文本宽度" class="headerlink" title="使用 fold 限制文本宽度"></a>使用 <code>fold</code> 限制文本宽度</h2><p>控制文件内容输出时所占用的屏幕宽度。</p>
<p><code>fold</code> 用于控制文件内容输出时所占用的屏幕宽度。<code>fold</code> 命令会从指定的文件里读取内容，将超过限定列宽的列加入增列字符后，输出到标准输出设备。若不指定任何文件名称，或是所给予的文件名为“-”，则 <code>fold</code> 指令会从标准输入设备读取数据。</p>
<h3 id="fold"><a href="#fold" class="headerlink" title="fold"></a>fold</h3><h4 id="语法-4"><a href="#语法-4" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs bash">用法：fold [选项]... [文件]...<br>Wrap input lines <span class="hljs-keyword">in</span> each FILE, writing to standard output.<br><br>如果没有指定文件，或者文件为<span class="hljs-string">&quot;-&quot;</span>，则从标准输入读取。<br><br>必选参数对长短选项同时适用。<br>  -b, --bytes		计算字节数而不是列数<br>  -s,  --spaces		在空格处断行<br>  -w, --width=宽度	使用指定的列宽度代替默认的80<br>      --<span class="hljs-built_in">help</span>		显示此帮助信息并退出<br>      --version		显示版本信息并退出<br></code></pre></td></tr></table></figure>

<p><strong>示例</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ fold -w 50 /tmp/a.txt  -s<br>TUBITAK Kamu SM SSL Kok Sertifikasi - Surum 1<br>=============================================<br>-----BEGIN CERTIFICATE-----<br>MIIEYzCCA0ugAwIBAgIBATANBgkqhkiG9w0BAQsFADCB0jELMA<br>kGA1UEBhMCVFIxGDAWBgNVBAcT<br>D0dlYnplIC0gS29jYWVsaTFCMEAGA1UEChM5VHVya2l5ZSBCaW<br>xpbXNlbCB2ZSBUZWtub2xvamlr<br>IEFyYXN0aXJtYSBLdXJ1bXUgLSBUVUJJVEFLMS0wKwYDVQQLEy<br>RLYW11IFNlcnRpZmlrYXN5b24g<br>TWVya2V6aSAtIEthbXUgU00xNjA0BgNVBAMTLVRVQklUQUsgS2<br>FtdSBTTSBTU0wgS29rIFNlcnRp<br>ZmlrYXNpIC0gU3VydW0gMTAeFw0xMzExMjUwODI1NTVaFw00Mz<br>EwMjUwODI1NTVaMIHSMQswCQYD<br>VQQGEwJUUjEYMBYGA1UEBxMPR2ViemUgLSBLb2NhZWxpMUIwQA<br>YDVQQKEzlUdXJraXllIEJpbGlt<br>c2VsIHZlIFRla25vbG9qaWsgQXJhc3Rpcm1hIEt1cnVtdSAtIF<br>RVQklUQUsxLTArBgNVBAsTJEth<br>bXUgU2VydGlmaWthc3lvbiBNZXJrZXppIC0gS2FtdSBTTTE2MD<br>QGA1UEAxMtVFVCSVRBSyBLYW11<br>IFNNIFNTTCBLb2sgU2VydGlmaWthc2kgLSBTdXJ1bSAxMIIBIj<br>ANBgkqhkiG9w0BAQEFAAOCAQ8A<br>MIIBCgKCAQEAr3UwM6q7a9OZLBI3hNmNe5eA027n/5tQlT6QlV<br>ZC1xl8JoSNkvoBHToP4mQ4t4y8<br>6Ij5iySrLqP1N+RAjhgleYN1Hzv/bKjFxlb4tO2KRKOrbEz8Hd<br>Dc72i9z+SqzvBV96I01INrN3wc<br>wv61A+xXzry0tcXtAA9TNypN9E8Mg/uGz8v+jE69h/mniyFXnH<br>rfA2eJLJ2XYacQuFWQfw4tJzh0<br>3+f92k4S400VIgLI4OD8D62K18lUUMw7D8oWgITQUVbDjlZ/iS<br>IzL+aFCr2lqBs23tPcLG07xxO9<br>WSMs5uWk99gL7eqQQESolbuT1dCANLZGeA4fAJNG4e7p+exPFw<br>IDAQABo0IwQDAdBgNVHQ4EFgQU<br>ZT/HiobGPN08VFw1+DrtUgxHV8gwDgYDVR0PAQH/BAQDAgEGMA<br>8GA1UdEwEB/wQFMAMBAf8wDQYJ<br>KoZIhvcNAQELBQADggEBACo/4fEyjq7hmFxLXs9rHmoJ0iKpEs<br>deV31zVmSAhHqT5Am5EM2fKifh<br>AHe+SMg1qIGf5LgsyX8OsNJLN13qudULXjS99HMpw+0mFZx+CF<br>OKWI3QSyjfwbPfIPP54+M638yc<br>lNhOT8NrF7f3cuitZjO1JVOr4PhMqZ398g26rrnZqsZr+ZO7rq<br>u4lzwDGrpDxpa5RXI4s6ehlj2R<br>e37AIVNMh+3yC1SVUZPVIqUNivGTDj5UDrDYyU7c8jEyVupk+e<br>q1nRZmQnLzf9OxMUP8pI4X8W0j<br>q5Rm+K37DwhuJi1/FwcJsoz7UMCflo3Ptv0AnVoUmr8CRPXBwp<br>8iXqIPoeM=<br>-----END CERTIFICATE-----<br></code></pre></td></tr></table></figure>

<p><code>fold</code> 命令的参数 <code>-w</code> 限定每行宽度为 50，超出这个宽度就会被截断到下一行输出。</p>
<h1 id="提取文本开头和结尾"><a href="#提取文本开头和结尾" class="headerlink" title="提取文本开头和结尾"></a>提取文本开头和结尾</h1><h3 id="head"><a href="#head" class="headerlink" title="head"></a>head</h3><h4 id="语法-5"><a href="#语法-5" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs bash">用法：head [选项]... [文件]...<br>默认显示文件的头 10 行内容到标准输出。<br>对于多个文件，在每个文件前面都加上一个给出文件名的头<br><br>如果没有指定文件，或者文件为<span class="hljs-string">&quot;-&quot;</span>，则从标准输入读取。<br><br>必选参数对长短选项同时适用。<br>  -c, --bytes=[-]NUM       <span class="hljs-built_in">print</span> the first NUM bytes of each file;<br>                             with the leading <span class="hljs-string">&#x27;-&#x27;</span>, <span class="hljs-built_in">print</span> all but the last NUM bytes of each file<br>  -n, --lines=[-]NUM       <span class="hljs-built_in">print</span> the first NUM lines instead of the first 10;<br>                             with the leading <span class="hljs-string">&#x27;-&#x27;</span>, <span class="hljs-built_in">print</span> all but the last NUM lines of each file<br>  -q, --quiet, --silent	不显示包含给定文件名的文件头<br>  -v, --verbose		总是显示包含给定文件名的文件头<br>  -z, --zero-terminated    line delimiter is NUL, not newline<br>      --<span class="hljs-built_in">help</span>		显示此帮助信息并退出<br>      --version		显示版本信息并退出<br></code></pre></td></tr></table></figure>

<p><code>head</code> 命令用来显示文件（或多个文件）的开头若干行。</p>
<h3 id="tail"><a href="#tail" class="headerlink" title="tail"></a>tail</h3><h4 id="语法-6"><a href="#语法-6" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs bash">用法：tail [选项]... [文件]...<br>Print the last 10 lines of each FILE to standard output.<br>With more than one FILE, precede each with a header giving the file name.<br><br>如果没有指定文件，或者文件为<span class="hljs-string">&quot;-&quot;</span>，则从标准输入读取。<br><br>必选参数对长短选项同时适用。<br>  -c, --bytes=[+]NUM       output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each file<br>  -f, --follow[=&#123;name|descriptor&#125;]<br>          output appended data as the file grows;<br>          an absent option argument means <span class="hljs-string">&#x27;descriptor&#x27;</span><br>  -F      same as --follow=name --retry<br>  -n, --lines=[+]NUM       output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM<br>      --max-unchanged-stats=N<br>                           with --follow=name, reopen a FILE <span class="hljs-built_in">which</span> has not changed size after N (default 5) iterations to see <span class="hljs-keyword">if</span> it has been unlinked or renamed (this is the usual <span class="hljs-keyword">case</span> of rotated <span class="hljs-built_in">log</span> files); with inotify, this option is rarely useful<br>      --pid=PID            with -f, terminate after process ID, PID dies<br>  -q, --quiet, --silent    never output headers giving file names<br>      --retry              keep trying to open a file <span class="hljs-keyword">if</span> it is inaccessible<br>  -s, --sleep-interval=N   with -f, sleep <span class="hljs-keyword">for</span> approximately N seconds<br>                             (default 1.0) between iterations;<br>                             with inotify and --pid=P, check process P at<br>                             least once every N seconds<br>  -v, --verbose            always output headers giving file names<br>  -z, --zero-terminated    line delimiter is NUL, not newline<br>      --<span class="hljs-built_in">help</span>		显示此帮助信息并退出<br>      --version		显示版本信息并退出<br></code></pre></td></tr></table></figure>

<p><code>tail</code> 命令和 <code>head</code> 命令的功能正好相反，用于显示文件的结尾若干行。参数也差不多。</p>
<p><strong>示例</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ head /usr/include/stdio.h<br>/* Define ISO C stdio on top of C++ iostreams.<br>   Copyright (C) 1991-2018 Free Software Foundation, Inc.<br>   This file is part of the GNU C Library.<br><br>   The GNU C Library is free software; you can redistribute it and/or<br>   modify it under the terms of the GNU Lesser General Public<br>   License as published by the Free Software Foundation; either<br>   version 2.1 of the License, or (at your option) any later version.<br><br>   The GNU C Library is distributed <span class="hljs-keyword">in</span> the hope that it will be useful,<br>$ head -5 /usr/include/stdio.h<br>/* Define ISO C stdio on top of C++ iostreams.<br>   Copyright (C) 1991-2018 Free Software Foundation, Inc.<br>   This file is part of the GNU C Library.<br><br>   The GNU C Library is free software; you can redistribute it and/or<br>$ tail /usr/include/stdio.h<br><span class="hljs-comment">#if __USE_FORTIFY_LEVEL &gt; 0 &amp;&amp; defined __fortify_function</span><br><span class="hljs-comment"># include &lt;bits/stdio2.h&gt;</span><br><span class="hljs-comment">#endif</span><br><span class="hljs-comment">#ifdef __LDBL_COMPAT</span><br><span class="hljs-comment"># include &lt;bits/stdio-ldbl.h&gt;</span><br><span class="hljs-comment">#endif</span><br><br>__END_DECLS<br><br><span class="hljs-comment">#endif /* &lt;stdio.h&gt; included.  */</span><br>$ tail -5  /usr/include/stdio.h<br><span class="hljs-comment">#endif</span><br><br>__END_DECLS<br><br><span class="hljs-comment">#endif /* &lt;stdio.h&gt; included.  */</span><br></code></pre></td></tr></table></figure>

<h1 id="字段处理"><a href="#字段处理" class="headerlink" title="字段处理"></a>字段处理</h1><h2 id="使用-cut-取出字段"><a href="#使用-cut-取出字段" class="headerlink" title="使用 cut 取出字段"></a>使用 <code>cut</code> 取出字段</h2><p>有时经常会遇到这样的问题：有一页电话号码簿，上面按顺序规则地写着姓名、地址、电话、备注等。此时我们只需要所有人的姓名和电话号码，如何解决呢？</p>
<p><code>cut</code> 命令被设计出来已解决这类问题。<code>cut</code> 命令可以从一个文本文件或文本流中提取文本列。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ cut -d <span class="hljs-string">&#x27;:&#x27;</span> -f 1,7 /etc/passwd | grep bash<br>root:/bin/bash<br>aurora:/bin/bash<br>$ cut -d <span class="hljs-string">&#x27;:&#x27;</span> -f 1,6,7 /etc/passwd | grep bash | cut -d <span class="hljs-string">&#x27;:&#x27;</span> -f 1,2<br>root:/root<br>aurora:/home/aurora<br></code></pre></td></tr></table></figure>

<p>这个例子中使用了 <code>cut</code> 的两个参数：</p>
<p><strong>-d ‘:’</strong> <code>-d</code> 参数规定了 <code>cut</code> 命令接受的字段分隔符。示例中的分隔符就是冒号（:）。</p>
<p><strong>-f 1,7</strong> <code>-f</code> 参数规定了 <code>cut</code> 命令获取的字段列。此处的 <code>-f 1,7</code> 使 <code>cut</code> 截取每行的第一列和第七列字段。</p>
<h3 id="cut"><a href="#cut" class="headerlink" title="cut"></a>cut</h3><h4 id="语法-7"><a href="#语法-7" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs bash">用法：cut [选项]... [文件]...<br>将每个文件中选定的行部分打印到标准输出。<br><br>如果没有指定文件，或者文件为<span class="hljs-string">&quot;-&quot;</span>，则从标准输入读取。<br><br>必选参数对长短选项同时适用。<br>  -b, --bytes=列表		只选中指定的这些字节<br>  -c, --characters=列表		只选中指定的这些字符<br>  -d, --delimiter=分界符	使用指定分界符代替制表符作为区域分界<br>  -f, --fields=列表		只选中指定的这些域；并打印所有不包含分界符的<br>				行，除非-s 选项被指定<br>  -n				(忽略)<br>      --complement		补全选中的字节、字符或域<br>  -s, --only-delimited		不打印没有包含分界符的行<br>      --output-delimiter=字符串	使用指定的字符串作为输出分界符，默认采用输入<br>				的分界符<br>  -z, --zero-terminated    line delimiter is NUL, not newline<br>      --<span class="hljs-built_in">help</span>		显示此帮助信息并退出<br>      --version		显示版本信息并退出<br><br>仅使用f -b, -c 或-f 中的一个。每一个列表都是专门为一个类别作出的，或者您可以用逗号隔<br>开要同时显示的不同类别。您的输入顺序将作为读取顺序，每个仅能输入一次。<br>Each range is one of:<br><br>  N     N<span class="hljs-string">&#x27;th byte, character or field, counted from 1</span><br><span class="hljs-string">  N-    from N&#x27;</span>th byte, character or field, to end of line<br>  N-M   from N<span class="hljs-string">&#x27;th to M&#x27;</span>th (included) byte, character or field<br>  -M    from first to M<span class="hljs-string">&#x27;th (included) byte, character or field</span><br></code></pre></td></tr></table></figure>

<h2 id="使用-join-连接字段"><a href="#使用-join-连接字段" class="headerlink" title="使用 join 连接字段"></a>使用 join 连接字段</h2><p>Linux 下的 <code>join</code> 可以连接不同的文件，使得具有相同 key 值的记录信息连接到一起。它会根据指定栏位，找到两个文件中指定栏位内容相同的行，将他们合并，并根据要求的格式输出内容。</p>
<p><strong>示例</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ cat product.txt<br>100 Jason Smith<br>200 John Doe<br>300 SanJay Gupta<br>400 Ashok Sharma<br>500 Aurora Lzdf<br><br>$ cat price.txt <br>100 <span class="hljs-variable">$5</span>,000<br>200 <span class="hljs-variable">$500</span><br>300 <span class="hljs-variable">$3</span>,000<br>400 <span class="hljs-variable">$1</span>,250<br>500 <span class="hljs-variable">$1</span>,000,000<br><br>$ join product.txt price.txt<br>100 Jason Smith <span class="hljs-variable">$5</span>,000<br>200 John Doe <span class="hljs-variable">$500</span><br>300 SanJay Gupta <span class="hljs-variable">$3</span>,000<br>400 Ashok Sharma <span class="hljs-variable">$1</span>,250<br>500 Aurora Lzdf <span class="hljs-variable">$1</span>,000,000<br><br></code></pre></td></tr></table></figure>

<p>在这个例子中，key 值相同的两行被连接成了一行。</p>
<p><strong>示例2</strong> <em>join 显示不匹配的行</em></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ cat product.txt<br>100 Jason Smith<br>150 Ollir zhang		<span class="hljs-comment"># ---------- 新增</span><br>200 John Doe<br>300 SanJay Gupta<br>350 uncle wang		<span class="hljs-comment"># ---------- 新增</span><br>400 Ashok Sharma<br>500 Aurora Lzdf<br><br>$ cat price.txt <br>100 <span class="hljs-variable">$5</span>,000<br>200 <span class="hljs-variable">$500</span><br>300 <span class="hljs-variable">$3</span>,000<br>400 <span class="hljs-variable">$1</span>,250<br>500 <span class="hljs-variable">$1</span>,000,000<br>600 <span class="hljs-variable">$3</span>,128			<span class="hljs-comment"># ---------- 新增</span><br><br>$ join product.txt price.txt -a1	<span class="hljs-comment"># ①</span><br>100 Jason Smith <span class="hljs-variable">$5</span>,000<br>150 Ollir zhang		<span class="hljs-comment"># ---------- 显示</span><br>200 John Doe <span class="hljs-variable">$500</span><br>300 SanJay Gupta <span class="hljs-variable">$3</span>,000<br>350 uncle wang		<span class="hljs-comment"># ---------- 显示</span><br>400 Ashok Sharma <span class="hljs-variable">$1</span>,250<br>500 Aurora Lzdf <span class="hljs-variable">$1</span>,000,000<br><br>$ join product.txt price.txt -a2	<span class="hljs-comment"># ②</span><br>100 Jason Smith <span class="hljs-variable">$5</span>,000<br>200 John Doe <span class="hljs-variable">$500</span><br>300 SanJay Gupta <span class="hljs-variable">$3</span>,000<br>400 Ashok Sharma <span class="hljs-variable">$1</span>,250<br>500 Aurora Lzdf <span class="hljs-variable">$1</span>,000,000<br>600 <span class="hljs-variable">$3</span>,128			<span class="hljs-comment"># ---------- 显示</span><br><br></code></pre></td></tr></table></figure>

<p>在这个例子中使用了 join 的一个参数：</p>
<p><code>-a FileNumber</code></p>
<p>该参数限定了 <code>join</code> 输出的记录行，由 <code>-a</code> 紧跟的 <code>FileNumber</code> 决定。<code>FileNumber</code> 必须是 1 或 2，分别对应于 <code>join</code> 命令的第一个文件参数和第二个文件参数。</p>
<p><strong>注释①：</strong> <code>-a1</code> 参数限定 <code>join</code> 的输出结果和 <code>product.txt</code> 中的记录一一对应。当 <code>price.txt</code> 匹配不上 key 时，则仅仅显示 <code>product.txt</code> 中的记录。</p>
<p><strong>注释②：</strong> <code>-a2</code> 参数限定 <code>join</code> 的输出结果和 price.txt` 中的记录一一对应。</p>
<h3 id="join"><a href="#join" class="headerlink" title="join"></a>join</h3><h4 id="语法-8"><a href="#语法-8" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs bash">用法：join [选项]... 文件1 文件2<br>对于具有相同连接字段的每对输入行，写一行到标准输出。默认连接字段是第一个，由空格分隔。<br><br>如果第一个文件参数或者第二个文件参数为 ‘-’，则从标准输入读取内容<br><br>  -a FILENUM        also <span class="hljs-built_in">print</span> unpairable lines from file FILENUM, <span class="hljs-built_in">where</span><br>                      FILENUM is 1 or 2, corresponding to FILE1 or FILE2<br>  -e EMPTY          replace missing input fields with EMPTY<br>  -i, --ignore-case  ignore differences <span class="hljs-keyword">in</span> <span class="hljs-keyword">case</span> when comparing fields<br>  -j FIELD          equivalent to <span class="hljs-string">&#x27;-1 FIELD -2 FIELD&#x27;</span><br>  -o FORMAT         obey FORMAT <span class="hljs-keyword">while</span> constructing output line<br>  -t CHAR           use CHAR as input and output field separator<br>  -v 文件编号        	类似 -a 文件编号，但禁止组合输出行<br>  -1 域          	在文件1 的此域组合<br>  -2 域          	在文件2 的此域组合<br>  --check-order     	检查输入行是否正确排序，即使所有输入行均是成对的<br>  --nocheck-order   	不检查输入是否正确排序<br>  --header          	将首行视作域的头部，直接输出而不对其进行匹配<br>  -z, --zero-terminated     line delimiter is NUL, not newline<br>      --<span class="hljs-built_in">help</span>		显示此帮助信息并退出<br>      --version		显示版本信息并退出<br></code></pre></td></tr></table></figure>

<h2 id="其他字段处理方法"><a href="#其他字段处理方法" class="headerlink" title="其他字段处理方法"></a>其他字段处理方法</h2><p>UNIX/Linux 下的字段处理远远不知 <code>cut</code> 和 <code>join</code> 两种命令，还有一个强大到甚至可以称之为语言的工具存在，他就是 <code>awk</code>。<code>awk</code> 的设计精髓在于字段与记录的处理上，使用它能够实现强大的字段处理功能。</p>
<h1 id="文本替换"><a href="#文本替换" class="headerlink" title="文本替换"></a>文本替换</h1><h2 id="使用-tr-替换字符"><a href="#使用-tr-替换字符" class="headerlink" title="使用 tr 替换字符"></a>使用 tr 替换字符</h2><p><code>tr</code> 命令从标准输入删除或替换字符，并将结果写入标准输出。在需要小范围文本替换时，<code>tr</code> 非常有用。</p>
<p><code>tr</code> 一般有两种格式：</p>
<ul>
<li><code>tr String1 String2</code> 将 String1 所包含的每个字符都替换成 String2 中相同位置上的字符。</li>
<li><code>tr &#123;-d | -s&#125; String1</code> <code>-d</code> 表示删除 String1中包含的每个字符；<code>-s</code> 标识删除包含在 String1 中的任何字符串系列中的中第一个字符以外的所有字符。</li>
</ul>
<p><strong>示例1</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ cat linux.wiki <br>Linux (/ˈlɪnəks/ (About this soundlisten) LIN-əks)[9][10] is a family of free and open-source software operating systems based on the Linux kernel,[11] an operating system kernel first released on September 17, 1991 by Linus Torvalds.[12][13][14] Linux is typically packaged <span class="hljs-keyword">in</span> a Linux distribution (or distro <span class="hljs-keyword">for</span> short).<br>Linux is one of the most prominent examples of free and open-source software collaboration. The <span class="hljs-built_in">source</span> code may be used, modified and distributed—commercially or non-commercially—by anyone under the terms of its respective licenses, such as the GNU General Public License.<br>$ tr <span class="hljs-string">&#x27;a-z&#x27;</span> <span class="hljs-string">&#x27;A-Z&#x27;</span> &lt;linux.wiki &gt;linux.wiki.upper<br>$ cat linux.wiki.upper<br>LINUX (/ˈLɪNəKS/ (ABOUT THIS SOUNDLISTEN) LIN-əKS)[9][10] IS A FAMILY OF FREE AND OPEN-SOURCE SOFTWARE OPERATING SYSTEMS BASED ON THE LINUX KERNEL,[11] AN OPERATING SYSTEM KERNEL FIRST RELEASED ON SEPTEMBER 17, 1991 BY LINUS TORVALDS.[12][13][14] LINUX IS TYPICALLY PACKAGED IN A LINUX DISTRIBUTION (OR DISTRO FOR SHORT).<br>LINUX IS ONE OF THE MOST PROMINENT EXAMPLES OF FREE AND OPEN-SOURCE SOFTWARE COLLABORATION. THE SOURCE CODE MAY BE USED, MODIFIED AND DISTRIBUTED—COMMERCIALLY OR NON-COMMERCIALLY—BY ANYONE UNDER THE TERMS OF ITS RESPECTIVE LICENSES, SUCH AS THE GNU GENERAL PUBLIC LICENSE.<br></code></pre></td></tr></table></figure>

<p>这个例子将所有的小写字母替换成大写字母。</p>
<p><strong>示例2</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs bash">$ tr <span class="hljs-string">&#x27;&#123;&#125;&#x27;</span> <span class="hljs-string">&#x27;()&#x27;</span> &lt; oldfile &gt; newfile	<span class="hljs-comment"># 将大括号替换为小括号</span><br>$ tr <span class="hljs-string">&#x27;&#123;&#125;&#x27;</span> <span class="hljs-string">&#x27;\[]&#x27;</span> &lt; oldfile &gt; newfile	<span class="hljs-comment"># 将大括号替换为中括号（反斜杠是否有必要？）</span><br>$ tr -cs <span class="hljs-string">&#x27;[:lower:][:upper:]&#x27;</span> <span class="hljs-string">&#x27;[\n*]&#x27;</span> &lt; oldfile &gt; newfile	<span class="hljs-comment"># 创建一个文件中的单词列表</span><br>$ tr -d <span class="hljs-string">&#x27;\0&#x27;</span> &lt; oldfile &gt; newfile	<span class="hljs-comment"># 从旧文件中删除所有空字符</span><br>$ tr -s <span class="hljs-string">&#x27;\n&#x27;</span> &lt; oldfile &gt; newfile	<span class="hljs-comment"># 用单独的换行符替换每个序列中的一个或多个换行</span><br>$ tr -s <span class="hljs-string">&#x27;\012&#x27;</span> &lt; oldfile &gt; newfile	<span class="hljs-comment"># 同上</span><br>$ tr -c <span class="hljs-string">&#x27;[:print:][:cntrl:]&#x27;</span> <span class="hljs-string">&#x27;[?*]&#x27;</span> &lt; oldfile &gt; newfile	<span class="hljs-comment"># 以”？“替换每个费打印字符（有效控制字符除外）</span><br>$ tr -s <span class="hljs-string">&#x27;[:space:]&#x27;</span> <span class="hljs-string">&#x27;[#*]&#x27;</span>	<span class="hljs-comment"># 以单个”#“ 字符替换 &lt;space&gt; 字符类中的每个字符序列</span><br></code></pre></td></tr></table></figure>

<h3 id="tr"><a href="#tr" class="headerlink" title="tr"></a>tr</h3><h4 id="语法-9"><a href="#语法-9" class="headerlink" title="语法"></a>语法</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs bash">用法：tr [选项]... SET1 [SET2]<br>Translate, squeeze, and/or delete characters from standard input,<br>writing to standard output.<br><br>  -c, -C, --complement    use the complement of SET1<br>  -d, --delete            delete characters <span class="hljs-keyword">in</span> SET1, <span class="hljs-keyword">do</span> not translate<br>  -s, --squeeze-repeats   replace each sequence of a repeated character that is listed <span class="hljs-keyword">in</span> the last specified SET, with a single occurrence of that character<br>  -t, --truncate-set1     first truncate SET1 to length of SET2<br>      --<span class="hljs-built_in">help</span>		显示此帮助信息并退出<br>      --version		显示版本信息并退出<br><br>SET 是一组字符串，一般都可按照字面含义理解。解析序列如下：<br>  \NNN	八进制值为NNN 的字符(1 至3 个数位)<br>  \\		反斜杠<br>  \a		终端鸣响<br>  \b		退格<br>  \f		换页<br>  \n		换行<br>  \r		回车<br>  \t		水平制表符<br>  \v		垂直制表符<br>  字符1-字符2	从字符1 到字符2 的升序递增过程中经历的所有字符<br>  [字符*]	在SET2 中适用，指定字符会被连续复制直到吻合设置1 的长度<br>  [字符*次数]	对字符执行指定次数的复制，若次数以 0 开头则被视为八进制数<br>  [:alnum:]	所有的字母和数字<br>  [:alpha:]	所有的字母<br>  [:blank:]	所有呈水平排列的空白字符<br>  [:cntrl:]	所有的控制字符<br>  [:digit:]	所有的数字<br>  [:graph:]	所有的可打印字符，不包括空格<br>  [:lower:]	所有的小写字母<br>  [:<span class="hljs-built_in">print</span>:]	所有的可打印字符，包括空格<br>  [:punct:]	所有的标点字符<br>  [:space:]	所有呈水平或垂直排列的空白字符<br>  [:upper:]	所有的大写字母<br>  [:xdigit:]	所有的十六进制数<br>  [=字符=]	所有和指定字符相等的字符<br></code></pre></td></tr></table></figure>

<h2 id="其他选择"><a href="#其他选择" class="headerlink" title="其他选择"></a>其他选择</h2><p><strong>perl</strong> 强大的这个表达式支持在 UNIX/Linux 世界无出其右。</p>
<p>**sed ** <code>sed</code> 工具处理文本流，亦可轻松地实现文本替换。</p>
<p><strong>awk</strong> <code>awk</code> 语言据有逻辑判断与循环等特性支持。并且，在文本处理时根据需求，强大的可定制性也是其长盛不衰的原因。</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/Shell/">Shell</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Linux/">Linux</a>
                    
                      <a class="hover-with-bg" href="/tags/Shell/">Shell</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2019/04/24/nginx/%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86%E4%B8%8E%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">【转】正向代理与反向代理</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2019/04/15/php/PHP-to-xml/">
                        <span class="hidden-mobile">PHP-to-xml</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
